package adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;

import activity.WorkOrderDetailActivity;
import dao.WorkOrderItem;

public class WorkOrderListAdapter extends RecyclerView.Adapter<WorkOrderListAdapter.WorkOrderVH> {

    private final Context mContext;
    private List<WorkOrderItem> mData;

    public WorkOrderListAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
    }

    public void setupData(List<WorkOrderItem> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public WorkOrderVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new WorkOrderVH(LayoutInflater.from(mContext).inflate(R.layout.item_work_order_list, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull WorkOrderVH holder, int position) {
        if (position < 0 || position >= mData.size()) {
            return;
        }
        WorkOrderItem item = mData.get(position);
        holder.workOrderNo.setText("工单号：" + item.workOrderNo);
        holder.workOrderType.setText("类型：" + item.workOrderType);
        holder.time.setText(item.gmtCreate);
        if (item.workOrderState == 1) {
            holder.status.setText("请于" + item.processTime + "个工作日内处理");
            holder.status.setTextColor(mContext.getResources().getColor(R.color.red_ff2f2f));
        } else if (item.workOrderState == 2) {
            holder.status.setText("待审核");
            holder.status.setTextColor(mContext.getResources().getColor(R.color.orange_f0ad59));
        } else if (item.workOrderState == 3) {
            holder.status.setText("处理失败");
            holder.status.setTextColor(mContext.getResources().getColor(R.color.red_ff2f2f));
        } else if (item.workOrderState == 4) {
            holder.status.setText("处理完成");
            holder.status.setTextColor(mContext.getResources().getColor(R.color.green_4dd28b));
        }

        holder.itemView.setOnClickListener(v -> WorkOrderDetailActivity.start(mContext, item.id));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class WorkOrderVH extends RecyclerView.ViewHolder {
        private TextView workOrderNo, workOrderType, time, status;

        WorkOrderVH(@NonNull View itemView) {
            super(itemView);
            workOrderNo = itemView.findViewById(R.id.work_order_no);
            workOrderType = itemView.findViewById(R.id.work_order_type);
            time = itemView.findViewById(R.id.time);
            status = itemView.findViewById(R.id.status);
        }
    }

}
